﻿using System;
using System.IO;
using System.Windows.Forms;
using OcrLiteLib;
using TextBox = System.Windows.Forms.TextBox;

namespace OcrAnswerQuestionsForm
{
    public partial class SensitiveOptionForm : AntdUI.Window
    {
        private OcrLite ocrEngin;
        public SensitiveOptionForm()
        {
            InitializeComponent();
            //AutoDpi(this);
        }

        private void initBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(@"是否确定需要重新初始化，请谨慎选择？", @"Message!", MessageBoxButtons.OKCancel) !=
                DialogResult.OK) return;
            // 保存 文本分析参数 配置数据
            // 数据校验
            if (!int.TryParse(paddingNumeric.Text, out _)
                || !int.TryParse(imgResizeNumeric.Text, out _)
                || !float.TryParse(boxScoreThreshNumeric.Text, out _)
                || !float.TryParse(boxThreshNumeric.Text, out _)
                || !float.TryParse(unClipRatioNumeric.Text, out _)
               )
            {
                MessageBox.Show(@"数据格式校验失败，请检查输入数据！");
                return;
            }

            // 重新初始化操作
            var detPath = $"{modelsTextBox.Text}\\{detNameTextBox.Text}";
            var clsPath = $"{modelsTextBox.Text}\\{clsNameTextBox.Text}";
            var recPath = $"{modelsTextBox.Text}\\{recNameTextBox.Text}";
            var keysPath = $"{modelsTextBox.Text}\\{keysNameTextBox.Text}";
            var isDetExists = File.Exists(detPath);
            if (!isDetExists)
            {
                MessageBox.Show(@"模型文件不存在:" + detPath);
            }
            var isClsExists = File.Exists(clsPath);
            if (!isClsExists)
            {
                MessageBox.Show(@"模型文件不存在:" + clsPath);
            }
            var isRecExists = File.Exists(recPath);
            if (!isRecExists)
            {
                MessageBox.Show(@"模型文件不存在:" + recPath);
            }
            var isKeysExists = File.Exists(keysPath);
            if (!isKeysExists)
            {
                MessageBox.Show(@"Keys文件不存在:" + keysPath);
            }
            if (isDetExists && isClsExists && isRecExists && isKeysExists)
            {
                ocrEngin = new OcrLite();
                ocrEngin.InitModels(detPath, clsPath, recPath, keysPath, (int)numThreadNumeric.Value);

                // 保存 训练模型设置 配置数据
                ConfigSettings.WriteSetting("modelsTextBox", modelsTextBox.Text.Trim());
                ConfigSettings.WriteSetting("detNameTextBox", detNameTextBox.Text.Trim());
                ConfigSettings.WriteSetting("clsNameTextBox", clsNameTextBox.Text.Trim());
                ConfigSettings.WriteSetting("recNameTextBox", recNameTextBox.Text.Trim());
                ConfigSettings.WriteSetting("keysNameTextBox", keysNameTextBox.Text.Trim());
                ConfigSettings.WriteSetting("numThreadNumeric", numThreadNumeric.Value.ToString());
                ConfigSettings.WriteSetting("paddingNumeric", paddingNumeric.Text.Trim());
                ConfigSettings.WriteSetting("imgResizeNumeric", imgResizeNumeric.Text.Trim());
                ConfigSettings.WriteSetting("boxScoreThreshNumeric", boxScoreThreshNumeric.Text.Trim());
                ConfigSettings.WriteSetting("boxThreshNumeric", boxThreshNumeric.Text.Trim());
                ConfigSettings.WriteSetting("unClipRatioNumeric", unClipRatioNumeric.Text.Trim());
                ConfigSettings.WriteSetting("doAngleCheckBox", doAngleCheckBox.Checked.ToString());
                ConfigSettings.WriteSetting("mostAngleCheckBox", mostAngleCheckBox.Checked.ToString());
                ConfigSettings.WriteSetting("partImgCheckBox", partImgCheckBox.Checked.ToString());
                ConfigSettings.WriteSetting("debugCheckBox", debugCheckBox.Checked.ToString());

                MessageBox.Show(@"重新初始化成功，重启应用后生效！");
            }
            else
            {
                MessageBox.Show(@"初始化失败，请确认模型文件夹和文件后，重新初始化！");
            }
        }

        private void SensitiveOptionForm_Load(object sender, EventArgs e)
        {
            // 加载 训练模型设置
            SetControlValue(detNameTextBox, "detNameTextBox", "dbnet.onnx");
            SetControlValue(clsNameTextBox, "clsNameTextBox", "angle_net.onnx");
            SetControlValue(recNameTextBox, "recNameTextBox", "crnn_lite_lstm.onnx");
            SetControlValue(keysNameTextBox, "keysNameTextBox", "keys.txt");
            SetControlValue(numThreadNumeric, "numThreadNumeric", 4);

            // 加载 文本分析参数
            SetControlValue(paddingNumeric, "paddingNumeric", 50);
            SetControlValue(imgResizeNumeric, "imgResizeNumeric", 800);
            SetControlValue(boxScoreThreshNumeric, "boxScoreThreshNumeric", 0.618F);
            SetControlValue(boxThreshNumeric, "boxThreshNumeric", 0.300F);
            SetControlValue(unClipRatioNumeric, "unClipRatioNumeric", 2.0F);
            SetControlValue(doAngleCheckBox, "doAngleCheckBox", true);
            SetControlValue(mostAngleCheckBox, "mostAngleCheckBox", true);

            // 加载 调试状态参数
            SetControlValue(partImgCheckBox, "partImgCheckBox", false);
            SetControlValue(debugCheckBox, "debugCheckBox", false);

            // 加载 模型目录
            var appPath = AppDomain.CurrentDomain.BaseDirectory;
            var modelsDir = appPath + "models";
            modelsTextBox.Text = modelsDir;
        }

        private void SetControlValue(IDisposable control, string XmlNodeName, object Default)
        {
            switch (control)
            {
                case NumericUpDown numericUpDown:
                {
                    decimal.TryParse(ConfigSettings.ReadSetting(XmlNodeName), out var dValue);
                    numericUpDown.Value = dValue;
                    break;
                }
                case TextBox textBox:
                {
                    var sValue = Default.ToString().Trim();
                    if (!string.IsNullOrEmpty(ConfigSettings.ReadSetting(XmlNodeName)))
                    {
                        sValue = ConfigSettings.ReadSetting(XmlNodeName);
                    }
                    textBox.Text = sValue;
                    break;
                }
                case CheckBox checkBox:
                {
                    var bValue = Default.ToString().Trim() == true.ToString();
                    if (!string.IsNullOrEmpty(ConfigSettings.ReadSetting(XmlNodeName)))
                    {
                        bValue = ConfigSettings.ReadSetting(XmlNodeName) == true.ToString();
                    }
                    checkBox.Checked = bValue;
                    break;
                }
            }
        }

        private void modelsBtn_Click(object sender, EventArgs e)
        {
            using (var dlg = new FolderBrowserDialog())
            {
                dlg.SelectedPath = $@"{Environment.CurrentDirectory}\models";
                if (dlg.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(dlg.SelectedPath))
                {
                    modelsTextBox.Text = dlg.SelectedPath;
                }
            }
        }
    }
}
